import NetManager from "./NetManager";
import SocketManager from "./socket/SocketManager";

/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-31 18:42:59
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2025-01-07 15:53:03
 */
export default class GameData {

  private static _instance: GameData = null;

  public static get ins(): GameData {
    if (!this._instance) {
      this._instance = new GameData();
    }
    return this._instance;
  }

  // UI相关
  public mainNode: cc.Node = null;
  public moneyPool: cc.NodePool = null;

  // 房间相关
  public roomId: string = null;
  public roomOwnerId: string = null;
  public isGameStart: boolean = false;
  public model: number = 1; // 1:单机模式 2:联机模式
  public list: any[] = [];
  public searchRoomId: string = null;
  public pageInfo: any = {
    page: 1,
    pageSize: 7,
    total: 0,
  }; //分页数据

  // 用户信息
  public username: string = "";
  public nickName: string = "";
  public avatar: string = "";
  public roleId: number = 0;
  public gameId: number = 4284;

  // 游戏状态
  public getMoney: number = 0;
  public rdDownTime: number = 3; // 准备倒计时
  public downTime: number = 20; // 游戏倒计时
  public isOver: boolean = false; // 回合结束
  public isOver2: boolean = false; // 游戏结束
  public gameState: any = {
    players: {}, // 玩家数据
    roomId: "", // 房间ID
    ownerId: "", // 房主ID
  }; // 游戏状态

  // 我的数据
  public myInfo: any = {
    isOwner: false,
    isMaster: false,
    rank: 0,
    score: 0,
    nickName: "",
    avatar: "",
  };


  public lastSubmittedScore: number = 0;

  /**
   * 是否为房主
   */
  public get isRoomOwner(): boolean {
    return this.username == this.gameState.ownerId
  }

  /**
   * 初始化用户数据
   */
  public initUserData(): void {
    const isDebug = CC_DEBUG;
    const defaultAvatar = cc.url.raw("resources/icon/bg_touxiang_wutouxiang.png");

    // 获取用户名
    this.username = NetManager.ins.getParam("username") ||
      Math.floor(Math.random() * 10000).toString();

    // 获取头像
    this.avatar = isDebug ?
      defaultAvatar :
      decodeURIComponent(NetManager.ins.getParam("uimg")) || defaultAvatar;

    // 获取昵称
    const randomNickname = "游客" + Math.random().toString(36).substring(2, 15);
    this.nickName = isDebug ?
      NetManager.ins.getParam("username") || randomNickname :
      decodeURIComponent(NetManager.ins.getParam("nname")) || randomNickname;

    // 获取搜索房间ID
    this.searchRoomId = NetManager.ins.getParam("searchRoomId") || null;

    console.log('用户名', this.username);
    console.log('房间ID', this.gameState.roomId);
    console.log('昵称', this.nickName);

  }

  /**
   * 获取房间用户数据
   * @param roomId - 房间ID (必填)
   * @param page 页码
   * @param limit 每页数量
   * @param sortBy 排序字段 排序字段 (score: 分数, lastActive: 最后活跃时间)
   * @param sortOrder - 排序顺序 (asc: 升序, desc: 降序)
   * @param currentUserId - 当前用户ID (必填)
   */
  getRoomUserData(page: number, limit: number, sortBy: string = "score", sortOrder: string = "desc", currentUserId: string) {
    console.log('获取房间用户数据', {
      roomId: GameData.ins.gameState.roomId,
      page: page,
      limit: limit,
      sortBy: sortBy,
      sortOrder: sortOrder,
      userId: currentUserId,
    }, GameData.ins.getMoney);

    if (GameData.ins.getMoney > 0) {
      sortBy = "score";
      sortOrder = "desc";
    }

    SocketManager.ins.socket.emit('get-paginated-sorted-users', {
      roomId: GameData.ins.gameState.roomId,
      page: page,
      limit: limit,
      sortBy: sortBy,
      sortOrder: sortOrder,
      userId: currentUserId,
    })
  }
}